<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AddrController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data=\DB::table("addr")->where("uid",\Cookie::get('user')['id'])->get();
        return view("home.user.addr",['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$request->all();

        $data['uid']=\Cookie::get('user')['id'];
        $res=\DB::table("addr")->insert($data);
        echo $res;

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data=\DB::table("addr")->where("id",$id)->first();
        $da=explode("-",$data['addr']);
        $data['addr1']=$da[0];
        $data['addr2']=$da[1];
        $data['addr3']=$da[2];

        return json_encode($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $uid=\Cookie::get('user')['id'];
       $res=\DB::table("user")->where("id",$uid)->update(['aid'=>$id]);
       echo $res;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=$request->except("_method");
        $res=\DB::table("addr")->where("id",$id)->update($data);
        if($res==1){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }

    /*
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res=\DB::table("addr")->where("id",$id)->delete();
        echo $res;
    }
}
